from PyQt5.QtWidgets import QWidget, QApplication, QToolBox, QVBoxLayout, QPushButton
from PyQt5.QtCore import pyqtSignal, Qt
import sys

class ListWidget(QToolBox):
    btnSelected = pyqtSignal(str)

    def __init__(self, parent=None):
        super(ListWidget, self).__init__(parent)
        self.currentchecked = None

        self.insertitem("鸡舍", ["\uf030  一号相机", ])
        self.insertitem("花园", ["\uf030  二号相机", ])
        self.insertitem("竹林", ["\uf030  三号相机", ])
        self.insertitem("鸭塘", ["\uf030  四号相机", ])

        StyleSheet = """
             QToolBoxButton {
                background-color: rgb(255, 255, 255);
              min-height: 30px; 
              border 1px;
            }
            QToolBox::tab {
                background-color: rgb(255, 255, 255, 50);
                border: 1px solid #C0C0C0;
                color: black;
            }

            QToolBox::tab:first {
                background-color: rgb(255, 255, 255, 50);
                
                color: black;
            }

            QToolBox::tab:last {
                background-color: rgb(255, 255, 255, 50);
               
                color: black;
            }

            QToolBox::tab:selected { /* italicize selected tabs */
                font: italic;
                color: black;
            }
            
            """
        #self.setStyleSheet(StyleSheet)
        self.layout().setSpacing(0)
        self.setMinimumWidth(150)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setStyleSheet(StyleSheet)
        #self.setStyleSheet("background-color:rgb(255, 255, 255,50); ")

    def init_toolBtn(self, name):
        ret = QPushButton()
        ret.setText(name)
        ret.setStyleSheet("""
        
        QPushButton{
        border: 1px solid ;
        
        border-style: solid;
        border-radius:0px;
        
        height:30
        }
        
          QPushButton:checked{     
        border: 1px solid ;  
       
        border-style: solid;  
        border-radius:2px; 
        border-left:3px solid green; 
        width: 40px;  
        height:30px;  
        padding:0 0px;  
        }
        
        QPushButton:hover{     
        border: 1px solid ;  
        
        border-style: solid;  
        border-radius:2px;  
        border-left:3px solid green;
        width: 40px;  
        height:30px;  
        padding:0 0px;  
        }  
        """)

        ret.setMinimumSize(60, 50)
        ret.setCheckable(True)

        #ret.setAutoExclusive(True)
        ret.setStyleSheet("background-color:rgb(255, 255, 255, 50); font-family:'FontAwesome'")
        return ret

    def oncheck(self, checked):
        if checked:
            if self.currentchecked is not None:
                if id(self.currentchecked) != id(self.sender()):
                    temp = self.currentchecked
                    self.currentchecked = self.sender()
                    temp.setChecked(False)
                    self.btnSelected.emit(self.sender().text())
            else:
                self.currentchecked = self.sender()
                self.btnSelected.emit(self.sender().text())
        else:
            if id(self.currentchecked) == id(self.sender()):
                self.sender().setChecked(True)

    def insertBtn(self, itemtext, btntext):
        widget, index = self.indexItem(itemtext)
        if widget:
            ret = self.init_toolBtn(btntext)
            ret.toggled.connect(self.oncheck)
            widget.layout().insertWidget(widget.layout().count() - 1, ret)

    def insertitem(self, itmetext, btnlist):
        widget = QWidget()
        #widget.setWindowFlags(Qt.FramelessWindowHint)
        #widget.setAttribute(Qt.WA_TranslucentBackground)
        layout = QVBoxLayout()
        widget.setLayout(layout)
        widget.setStyleSheet("background-color:rgba(255, 255, 255, 20); Margin:0px;")
        for each in btnlist:
            if each is not None:
                ret = self.init_toolBtn(each)
                ret.toggled.connect(self.oncheck)
                layout.addWidget(ret)
        layout.addStretch()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        self.addItem(widget, itmetext)

    def rmItem(self, text):
        widget, index = self.indexItem(text)
        if widget:
            self.removeItem(index)
            widget.deleteLater()

    def rmBtn(self, itemtext, btntext):
        layout, btn = self.indexBtn(itemtext, btntext)
        if layout:
            layout.removeWidget(btn)
            btn.deleteLater()
            #print(layout.count())
            if layout.count() == 1:
                self.rmItem(itemtext)
            if id(btn) == id(self.currentchecked):
                self.currentchecked = None

    def indexItem(self, id):
        count = self.count()

        for each in range(count):
            text = self.itemText(each)
            if text == id:
                return self.widget(each), each

        return False, False

    def indexBtn(self, itemid, btnid):
        ret, itemindex = self.indexItem(itemid)
        if ret:
            widget = self.widget(itemindex)
            childlist = widget.children()
            for each in childlist:
                if type(each) == type(QPushButton()):
                    if each.text() == btnid:
                        return widget.layout(), each

        return False, False

    def currentChecked(self):
        if self.currentchecked is not None:
            return self.currentchecked

        return None

def main():
    app = QApplication(sys.argv)

    a = QWidget()
    ui = ListWidget(a)
    ui.layout().setSpacing(0)
    ui.resize(200, a.height())
    ui.rmBtn("正兴农场", "相机1")
    # ui.rmBtn("正兴农场", "相机2")
    # ui.rmBtn("正兴农场", "相机3")
    # ui.insertBtn("正兴农场", "相机6")
    # ui.rmItem("祥韵农场")
    # ui.test()

    a.show()

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
